package com.lin.chapter35;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 工厂方法模式和策略模式
 */
public class Client {
    public static void main(String[] args) {
        Card card = initIC();
        System.out.println("============初始化卡信息==========");
        showCard(card);
        boolean flag = true;
        while (flag){
            Trade trade = createTrade();
            DeductionFacade.deduct(card, trade);
            System.out.println("\n============交易凭证==========");
            System.out.println(trade.getTradeNo() + "交易成功！");
            System.out.println("本次发生的交易金额为：" + trade.getAmount()/100.0 + " 元");
            showCard(card);
            System.out.print("\n是否需要退出？(Y/N)");
            if (getInput().equalsIgnoreCase("y")){
                flag = false;
            }
        }
    }

    public static Card initIC(){
        Card card = new Card();
        card.setCardNo("11000010001010");
        card.setFreeMoney(100000);
        card.setSteadyMoney(80000);
        return card;
    }

    private static Trade createTrade(){
        Trade trade = new Trade();
        System.out.println("请输入交易编号：");
        trade.setTradeNo(getInput());
        System.out.println("请输入交易金额：");
        trade.setAmount(Integer.parseInt(getInput()));
        return trade;
    }

    public static void showCard(Card card){
        System.out.println("IC卡编号：" + card.getCardNo());
        System.out.println("固定类型余额：" + card.getSteadyMoney()/100.0 + " 元");
        System.out.println("自由类型余额：" + card.getFreeMoney()/100.0 + " 元");
    }

    public static String getInput(){
        String str = "";
        try{
            str = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        return str;
    }
}
